<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Dating_tipsController
 *
 * @author tuancasi
 */
class Dating_tipsController extends Controller {

    public function actionIndex() {
	$model = new Tip('search');
	$model->unsetAttributes();
	if (isset($_GET['Tip'])) {
	    $model->attributes = $_GET['Tip'];
	}

	$this->render('index', array('model' => $model));
    }

    public function actionUpdate($id) {
	$model = $this->_loadModel($id);
	if (isset($_POST['submit'])) {
	    $model->attributes = $_POST['Tip'];
	    if ($model->validate()) {
		$model->user_id = user()->id;
		$model->time_created = time();
		$model->save();
		Yii::app()->user->setFlash('success', 'You have added the tip successfully.');
	    }
	}
	$this->render('update', array(
	    'model' => $model
	));
    }

    public function actionAdd() {
	$model = new Tip();
	if (isset($_POST['submit'])) {
	    $model->attributes = $_POST['Tip'];
	    if ($model->validate()) {
		$model->user_id = user()->id;
		$model->time_created = time();
		$model->save();
		Yii::app()->user->setFlash('success', 'You have added the tip successfully.');
		$this->redirect(url('dating_tips'));
	    }
	}
	$this->render('add', array('model' => $model));
    }

    public function actionDelete($id) {
	$model = $this->_loadModel($id);
	$model->delete();
    }

    private function _loadModel($id) {
	$model = Tip::model()->findByPk($id);
	if ($model) {
	    return $model;
	} else {
	    throw new CHttpException(400, "This page is not found!");
	}
    }

}

